<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
class Base extends Controller
{ 
  public function __construct()
  {
    $theme = Db::name("system")->where("config","themes")->find();
    if(isMobile())
    {   
      config('template.view_path', 'themes/' .$theme['extend']  .'/');
    }
    else{
      config('template.view_path','themes/' .$theme['value'] .'/');
    }
    parent::__construct();
  }
    public function _initialize()
    {
        $this->auth();
        $data=db('nav')->order("order desc")->select();
        $nav_main = array();
        foreach ($data as $item){
            $nav_main[$item["parent_id"]][] = $item;

        }
        $siteInfo= Db::name("system")->where("config","siteInfo")->find();
        $tag= Db::name("category")->order( 'id desc')->select();;//热门标签
        $hot_list = Db::name('article')->alias('c')->join('user u','c.uid = u.uid')->order("read_count desc")->paginate(6);//热门文章
        $link = Db::name("link")->select();
        //随机推荐文章
        $pk = Db::name("article")->getPK();//获取主键
        $countcus = Db::name("article")->field($pk)->select();//查询数据
        $con = '';
        $qu = '';
        foreach($countcus as $v=>$val){
            $con.= $val[$pk].'|';
        }
        $array = explode("|",$con);
        $countnum = count($array)-1;
        for($i = 0;$i <= 5;$i++){
            $sunum = mt_rand(0,$countnum);
            $qu.= $array[$sunum].',';
        }
        $random_list = Db::name('article')->alias('c')->join('user u','c.uid = u.uid')->where($pk,'in',$qu)->select();;
        if(session("loginUid")){
            $userInfo = Db::name("user")->where("uid",session("loginUid"))->find();
        }else{
            $userInfo = null;
        }
        //自定义JS代码
        $statistics = Db::name("system")->where("config","statistics")->value("value");
        $this->assign([
            'nav'=>$nav_main,
            'siteInfo'=>json_decode($siteInfo["value"],true),
            'link'=>$link,
            'userInfo'=>$userInfo,
            'tag'=>$tag ,
            'hot_list' => $hot_list,
            'random_list'=> $random_list,
            'statistics' => $statistics,
        ]);
    }
  
    public function auth()
    {
        $module = request()->module();
        $controller = request()->controller();
        $action = request()->action();
        $auth = new \app\admin\controller\Auth();
        if(!$auth->check($module .'/'.$controller . '/' . $action, session('loginUid'))){
            $this->error('你没有权限访问',"/");
        }
    }
}